/*
	Copyright 2009 Benjamin Vedder	vedder87@gmail.com
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    */

package com.google.code.retrobox.editor.commons;

import java.awt.Color;

public class Tools {
	private static final Color[] palette = {	new Color(Integer.parseInt( "000000",16)), 
			new Color(Integer.parseInt( "0000AA",16)),
			new Color(Integer.parseInt( "00AA00",16)),
			new Color(Integer.parseInt( "00AAAA",16)),
			new Color(Integer.parseInt( "AA0000",16)),
			new Color(Integer.parseInt( "AA00AA",16)),
			new Color(Integer.parseInt( "AA5500",16)),
			new Color(Integer.parseInt( "AAAAAA",16)),
			new Color(Integer.parseInt( "555555",16)),
			new Color(Integer.parseInt( "5555FF",16)),
			new Color(Integer.parseInt( "55FF55",16)),
			new Color(Integer.parseInt( "55FFFF",16)),
			new Color(Integer.parseInt( "FF5555",16)),
			new Color(Integer.parseInt( "FF55FF",16)),
			new Color(Integer.parseInt( "FFFF55",16)),
			new Color(Integer.parseInt( "FFFFFF",16))
			};
	
	public static Color getColor(int i) {
		return palette[i];
	}
	
	public static int getColorAsRGB15(int i){
		return rgb(getColor(i).getRGB());
	}
	
	public static int getColorIndex(Color c) {
		for (int i = 0;i < 16;i++) {
			if (palette[i].getRGB() == c.getRGB()) {
				return i;
			}
		}
		return -1;
	}
	
	// Convert from 24-bit to 15-bit color
	public static int rgb(int c) {
		int r = (c&0xf80000)>>19;
		int g = (c&0xf800)>>6;
		int b = (c&0xf8)<<7;
		return b | g | r;
	}
	
	// Get red component of a 15-bit color
	public static int red(int x) {
		return (x&31)<<3;
	}
	
	// Get green component of a 15-bit color
	public static int green(int x) {
		return (x>>2)&0xf8;
	}
	
	// Get blue component of a 15-bit color
	public static int blue(int x) {
		return (x>>7)&0xf8;
	}
	
	public byte getTileData(){
		return 0;
	}

}
